/*
* Amenities Module Add New Location Model JS
*
* JSLint.com Check:
*/

RM.Pages.Amenities_Edit_ID = {
    xtype: "hidden",
    id: "rm_module_amenities_edit_id",
    name: "rm_module_amenities_edit_id"
};

// this is used to store the image value
RM.Pages.Amenities_IconURL = {
    xtype: "hidden",
    id: "rm_module_amenities_edit_icon_value",
    name: "rm_module_amenities_edit_icon_value"
};

// form panels

// language combo
RM.Pages.Functions.Amenities_Edit_Change_Language = function(combo, record, index){

    var dataArray = RM.Pages.Amenities_Edit_Language_Details_Array; // just shorten the variable length for this method to make it easier to read
    var i = 0;for (i; i < dataArray.length; i++ ){
        if (combo.value === dataArray[i].iso){
            Ext.getCmp("rm_module_amenities_edit_name").setValue(dataArray[i].name);
        }
    }
};

RM.Pages.Amenities_Edit_Language_Combo = new Ext.form.ComboBox({
    id: "edit_amenities_edit_iso",
    hiddenName: "edit_amenities_edit[iso]",
    xtype: "combo",
    typeAhead: true,
    fieldLabel: RM.Translate.Common.Languages,
    forceSelection: true,
    triggerAction: 'all',
    selectOnFocus: true,
    store: RM.Languages,
    bodyBorder: false,
    frame: false,
    width: 180,
    listeners: {
        'select': RM.Pages.Functions.Amenities_Edit_Change_Language
    }
});

RM.Pages.Amenities_Edit_Locale_Fieldset = {
        xtype: "fieldset",
        title: RM.Translate.Admin.Amenities.Edit.Locale,
        autoHeight: true,
        layout: 'form',
        autoWidth: true,
        bodyBorder: false,
        items: [
            RM.Pages.Amenities_Edit_Language_Combo
        ]
};
RM.Pages.Amenities_Edit_Fieldset = {
        xtype: "fieldset",
        title: RM.Translate.Admin.Amenities.Edit.Info,
        autoHeight: true,
        layout: 'form',
        autoWidth: true,
        bodyBorder: false,
        items: [
            {
                xtype: "textfield",
                id: "rm_module_amenities_edit_name",
                name: "rm_module_amenities_edit_name",
                fieldLabel: RM.Translate.Admin.Amenities.Edit.Name,
                width: 330
            },{
                xtype: "textfield",
                id: "rm_module_amenities_edit_info_url",
                name: "rm_module_amenities_edit_info_url",
                fieldLabel: RM.Translate.Admin.Amenities.Edit.Link,
                width: 330
            },{
                xtype: "panel",
                layout: 'border',
                height: 100,
                bodyStyle: "padding:10px",
                items: [{
                    xtype: "fieldset",
                    region: "east",
                    id: "rm_module_amenities_edit_icon",
                    name: "rm_module_amenities_edit_icon",
                    title: RM.Translate.Admin.Amenities.Edit.Icon,
                    height: 100,
                    width: 100
                },{
                    id : 'rm_module_amenities_img_upload_panel',
                    url : RM.Common.AssembleURL({
                        controller: 'Amenities',
                        action: 'imguploadjson'
                    }),
                    fileUpload: true,
                    trackResetOnLoad: true,
                    xtype: 'form',
                    region: "center",
                    autoWidth: true,
                    frame: false,
                    border: false,
                    bodyStyle: "padding:10px",
                    items:[{
                        xtype : 'fileuploadfield',
                        region: 'center',
                        allowBlank : true,
                        id: 'rm_module_amenities_edit_new_icon',
                        emptyText: RM.Translate.Admin.System.MediaManager.ChooseImage,
                        fieldLabel: RM.Translate.Admin.Amenities.Edit.IconFile,
                        name: 'extension_media_upload'
                        ,width:230
                    },{
                        xtype: 'button',
                        frame: false,
                        border: false,
                        region: 'east',
                        text: RM.Translate.Common.Upload,
                        handler: function(){
                            if(Ext.getCmp('rm_module_amenities_img_upload_panel').getForm().isValid()){
                                Ext.getCmp('rm_module_amenities_img_upload_panel').getForm().submit({
                                    success: function(form, action) {
                                        var dataObject = Ext.util.JSON.decode(action.response.responseText);
                                        var image = dataObject.filename;
                                        Ext.getCmp('rm_module_amenities_edit_icon').update("<div class='rm-module-amenities-icon-panel'><img src='" + RM.UserRootURL + "RM/userdata/images/Amenities/" + image + "'></div>");
                                        Ext.getCmp('rm_module_amenities_edit_icon_value').setValue(image);
                                    },
                                    failure: function(form, action){
                                        Ext.Msg.alert(RM.Translate.Common.Success, RM.Translate.Admin.System.MediaManager.UploadFailure);
                                    },
                                    waitMsg: RM.Translate.Common.Uploading,
                                    waitTitle: RM.Translate.Common.PleaseWait
                                });
                            }
                        }
                    }]
                }]
            }
        ]
};
RM.Pages.Amenities_Edit_Units_Fieldset = {
        xtype: "fieldset",
        title: RM.Translate.Admin.Amenities.Edit.AssignUnits,
        autoHeight: true,
        layout: 'form',
        autoWidth: true,
        bodyBorder: false,
        items: [
            RM.Pages.Functions.Units_Selector
        ]
};

// the main panel/window
RM.Pages.Amenities_Edit_MainPanel = new Ext.Window({
    id: 'rm_pages_amenities_edit_add_mainpage',
    title: RM.Translate.Admin.Amenities.Edit.AddEdit,
    layout: 'fit',
    width: 500,
    height: 500,
    autoDestroy: false,
    closable: false,
    model: true,
    items: [
        RM.Pages.Amenities_Edit_ID,
        RM.Pages.Amenities_IconURL,
        RM.Pages.Amenities_Edit_Locale_Fieldset,
        RM.Pages.Amenities_Edit_Fieldset,
        RM.Pages.Amenities_Edit_Units_Fieldset
    ],
    listeners: {
        'afterlayout': function(){

            // clear the feilds
            Ext.getCmp('rm_module_amenities_edit_id').setValue("");
            Ext.getCmp('rm_module_amenities_edit_name').setValue("");
            Ext.getCmp('rm_module_amenities_edit_info_url').setValue("");
            Ext.getCmp('rm_module_amenities_edit_icon').innerHTML = "";
            Ext.getCmp('rm_module_amenities_edit_icon_value').setValue('');

            var languageSelection = Ext.getCmp('edit_amenities_edit_iso');
            languageSelection.setValue(RM.Language);

            // get the list selection
            var selection = RM.Pages.Amenities_List_Columns_SM.getSelections();
            if (selection.length>0){
                selection = selection[0].id;
            } else {
                selection = null
                return;
            }

            // clear the list selection
            RM.Pages.Amenities_List_Columns_SM.clearSelections();

            var myMask = new Ext.LoadMask('rm_pages_amenities_edit_add_mainpage', {msg:RM.Translate.Common.PleaseWait});
            myMask.show();



            var conn = new Ext.data.Connection();
            var request = {
                url: RM.Common.AssembleURL({
                    controller: 'Amenities',
                    action: 'loadjson',
                    parameters: [
                        {
                            name: 'id',
                            value: selection
                        },{
                            name: 'language',
                            value: languageSelection.getValue()
                        }
                    ]
                }),
                method: 'POST',
                success: function(responseObject) {
                    var jsonObject = Ext.util.JSON.decode(responseObject.responseText);

                    if (jsonObject.data.id !== "" || jsonObject.data.id !== undefined){

                        var isoName;
                        eval("isoName = jsonObject.data." + RM.Language);

                        //RM.Languages // all installed languages
                        RM.Pages.Amenities_Edit_Language_Details_Array = []; // global array to store our current data
                        var i = 0;for (i; i < RM.Languages.length; i++ ){
                            var languageData = {}; // create object
                            languageData.iso = RM.Languages[i][0];
                            eval ("languageData.name = jsonObject.data." + RM.Languages[i][0]);
                            RM.Pages.Amenities_Edit_Language_Details_Array.push(languageData);
                        }

                        Ext.getCmp('rm_module_amenities_edit_id').setValue(jsonObject.data.id);
                        Ext.getCmp('rm_module_amenities_edit_name').setValue(isoName);
                        Ext.getCmp('rm_module_amenities_edit_info_url').setValue(jsonObject.data.info_url);

                        if (jsonObject.data.icon_url!==undefined){
                            var image = jsonObject.data.icon_url;
                            Ext.getCmp('rm_module_amenities_edit_icon').update("<div class='rm-module-amenities-icon-panel'><img src='" + RM.UserRootURL + "RM/userdata/images/Amenities/" + image + "'></div>");
                            Ext.getCmp('rm_module_amenities_edit_icon_value').setValue(image);
                        }

                        RM.Pages.Functions.Units_Selector_SetSelection(jsonObject.data.units, jsonObject.totalUnits);
                    }

                    myMask.hide();

                },
                failure: function() {
                    Ext.MessageBox.alert(RM.Translate.Common.Failed);
                    myMask.hide();
                }
            };
            conn.request(request);

            RM.Help.Load('Admin.Amenities.Edit');
        }
    },
    buttons: [
        {
            text: RM.Translate.Common.Save,
            handler: function(){
                RM.Pages.Functions.Amenities_Edit_Save();
            }
        },{
            text: RM.Translate.Common.Cancel,
            handler: function(){
                RM.Pages.Amenities_Edit_MainPanel.hide();
            }
        }
    ]
});

// functions

RM.Pages.Functions.Amenities_Edit_Save = function(){

    var myMask = new Ext.LoadMask('rm_pages_amenities_edit_add_mainpage', {msg:RM.Translate.Common.PleaseWait});
    myMask.show();

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller: 'Amenities',
            action: 'updatejson'
        }),
        params:{
            id: Ext.getCmp('rm_module_amenities_edit_id').getValue(),
            name: Ext.getCmp('rm_module_amenities_edit_name').getValue(),
            info_url: Ext.getCmp('rm_module_amenities_edit_info_url').getValue(),
            icon: Ext.getCmp('rm_module_amenities_edit_icon_value').getValue(),
            units: RM.Pages.Functions.Units_Selector_GetSelection(),
            language: Ext.getCmp('edit_amenities_edit_iso').getValue()
        },
        method: 'POST',
        success: function(responseObject) {
            var jsonObject = RM.Common.JSON.decode(responseObject.responseText, true);
            RM.Pages.Amenities_Edit_MainPanel.hide();
            RM.Pages.Amenities_List_Json_Store.reload();
            myMask.hide();
        },
        failure: function() {
            Ext.MessageBox.alert(RM.Translate.Common.Failed);
            myMask.hide();
        }
    };
    conn.request(request);
}
